package com.qk.controller;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/courses")
public class CourseController {
    @Autowired
    private CourseService courseService;
    // 1. 课程列表查询
    @RequestMapping
    public Result listCourses(String name, Integer subject,Integer target,
  @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        //System.out.println("查询部门：" + name + " " + status + " " + page + " " + pageSize);
        log.info("分页查询部门：name={},subject={},target={},page={},pageSize={}", name, subject, target, page, pageSize);
        PageResult<Course> pageResult = courseService.selectlist(name, subject, target, page, pageSize);
        return Result.success(pageResult);
    }
    //2. 添加课程
    @PostMapping
    public Result addCourse(@RequestBody Course course) {
        log.info("添加课程：{}", course);
        courseService.addCourse(course);
        return Result.success();
    }
    //3、根据ID查询课程数据
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("查询课程ID为数据:{}", id);
        Course course = courseService.getById(id);
        return Result.success(course);
    }
   // 4. 修改课程
    @PutMapping
    public Result update(@RequestBody Course course) {
        log.info("修改课程：{}", course);
        courseService.update(course);
        return Result.success();
    }
//5. 删除课程
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除课程：{}", id);
        courseService.delete(id);
        return Result.success();
    }
//6、查询所有课程
    @GetMapping("/list")
    public Result getlist() {
        log.info("查询所有课程");

        return Result.success(courseService.listCourses());
    }
    //7. 根据学科查询课程列表
    @GetMapping("/subject/{subject}")
    public Result getCourseBySubject(@PathVariable Integer subject) {
        log.info("根据学科查询课程列表：{}", subject);
        List<Course> courses = courseService.getCourseBySubject(subject);
        return Result.success(courses);
    }
}
